//package com.inspur.edp.cef.generator.coreGenerator.base.serializer;
//
//import com.fasterxml.jackson.core.JsonGenerator;
//import com.fasterxml.jackson.databind.SerializerProvider;
//import com.inspur.edp.bef.bizentity.GspBusinessEntity;
//import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
//import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
//import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
//import com.inspur.edp.cef.designtime.api.IGspCommonField;
//import com.inspur.edp.cef.designtime.api.collection.GspFieldCollection;
//import com.inspur.edp.cef.entity.entity.ICefData;
//import com.inspur.edp.cef.generator.coreGenerator.base.entityGenerator.CefDataTypeImplGeneratorContext;
//import com.inspur.edp.cef.generator.coreGenerator.entityGenerator.serializer.CMChangeJsonConvertorGenUtils;
//import com.inspur.edp.cef.generator.overall.GenUtils;
//import com.inspur.edp.cef.generator.overall.Utils;
//import com.inspur.edp.cef.spi.jsonser.base.StringUtils;
//import com.inspur.edp.das.commonmodel.IGspCommonElement;
//import com.inspur.edp.das.commonmodel.IGspCommonObject;
//import java.util.ArrayList;
//import java.util.List;
//import org.eclipse.jdt.core.dom.AST;
//import org.eclipse.jdt.core.dom.Block;
//import org.eclipse.jdt.core.dom.CastExpression;
//import org.eclipse.jdt.core.dom.Expression;
//import org.eclipse.jdt.core.dom.IfStatement;
//import org.eclipse.jdt.core.dom.InfixExpression;
//import org.eclipse.jdt.core.dom.MethodInvocation;
//import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
//import org.eclipse.jdt.core.dom.Statement;
//import org.eclipse.jdt.core.dom.StringLiteral;
//import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
//import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
//
////
////     private void  writeXXAssociation(JsonWriter writer, object value, JsonSerializer serializer)
////     {
////        JsonWriter.WriteStar
////     }
////
//public class WriteAssociationGenerator extends ClassMethodGenerator {
//
//  protected static final String ParamValue = "assoValue";
//  protected static final String ParamWriter = "writer";
//  protected static final String ParamSerializer = "serializer";
//  protected static final String AssoValName = "info";
//
//  private IGspCommonField element;
//  private CefDataTypeImplGeneratorContext dataTypeGenContext;
//  private IGspCommonField refElement;
//
//  public WriteAssociationGenerator(
//      IGspCommonField element, CefDataTypeImplGeneratorContext dataTypeGenContext) {
//    this.element = element;
//    this.dataTypeGenContext = dataTypeGenContext;
//    this.dataTypeGenContext
//        .getAssociationWriteMetdNames()
//        .put(element.getLabelID(), getMethodName());
//    refElement = getRefElement(element);
//  }
//
//  @Override
//  protected final String getMethodName() {
//    return "write" + element.getLabelID() + "Association";
//  }
//
//  @Override
//  protected TypeInfo getReturnType() {
//    return new TypeInfo("void");
//  }
//
//  @Override
//  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
//    return new java.util.ArrayList<ModifierKeyword>(
//        java.util.Arrays.asList(new ModifierKeyword[]{ModifierKeyword.PRIVATE_KEYWORD}));
//  }
//
//  @Override
//  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
//    ParameterInfo tempVar = new ParameterInfo();
//    tempVar.setParamName(ParamWriter);
//    tempVar.setParamType(
//        com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(JsonGenerator.class));
//    ParameterInfo tempVar2 = new ParameterInfo();
//    tempVar2.setParamType(com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(Object.class));
//    tempVar2.setParamName(ParamValue);
//    ParameterInfo tempVar3 = new ParameterInfo();
//    tempVar3.setParamType(com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(
//        SerializerProvider.class));
//    tempVar3.setParamName(ParamSerializer);
//    return new java.util.ArrayList<ParameterInfo>(
//        java.util.Arrays.asList(new ParameterInfo[]{tempVar, tempVar2, tempVar3}));
//  }
//
//  @Override
//  protected Block buildMethodBody() {
//    //		Object user1Info = (Object)value ;
//    //		if (user1Info == null)
//    //		{
//    //			return;
//    //		}
//    //
//    //			this.WriteString(writer, (object)user1Info.User1_Code, "User1_Code", serializer);
//    //			this.WriteNestedValue(writer, serializer,
//    // "Inspur.GS.DemoScm.DemoSD.DemoSalesOrderTestDeploy.udt.TestRequired.TestRequired",
//    // user1Info.getUser1_TestSingleUdt(), "User1_TestSingleUdt");
//
//    java.util.ArrayList<Statement> statements = new java.util.ArrayList<Statement>();
//
//    // var info = (xxxInfo)assoValue;
//    VariableDeclarationStatement initStatement = getInitStatement();
//    statements.add(initStatement);
//
//    // if (info == null)
//    // writeAssociation(writer, assoValue, "ass1", serializer);
//    // return;
//    java.util.ArrayList<Statement> beforeReturnlist = new java.util.ArrayList<Statement>();
//    MethodInvocation writeAssoMethodInvocation = GenUtils.getMethodInvocation(ast, null,
//        CMChangeJsonConvertorGenUtils.mName_WriteAssociation);
//    writeAssoMethodInvocation.arguments().add(ast.newSimpleName(ParamWriter));
//    writeAssoMethodInvocation.arguments().add(ast.newSimpleName(AssoValName));
//    StringLiteral writeAssoMethodLabelIdLiteral = ast.newStringLiteral();
//    writeAssoMethodLabelIdLiteral.setLiteralValue(StringUtils.toCamelCase(element.getLabelID()));
//    writeAssoMethodInvocation.arguments().add(writeAssoMethodLabelIdLiteral);
//    writeAssoMethodInvocation.arguments().add(ast.newSimpleName(ParamSerializer));
//    beforeReturnlist.add(ast.newExpressionStatement(writeAssoMethodInvocation));
//    org.eclipse.jdt.core.dom.Statement nullReturnStatement = getNullReturnStatement(
//        beforeReturnlist);
//    statements.add(nullReturnStatement);
//
//    // writeFieldName(writer, "ass1");
//    MethodInvocation methodInvocation = GenUtils.getMethodInvocation(ast, null,
//        CMChangeJsonConvertorGenUtils.mName_WriteFieldName);
//    methodInvocation.arguments().add(ast.newSimpleName(ParamWriter));
//    StringLiteral labelIdLiteral = ast.newStringLiteral();
//    labelIdLiteral.setLiteralValue(StringUtils.toCamelCase(element.getLabelID()));
//    methodInvocation.arguments().add(labelIdLiteral);
//    statements.add(ast.newExpressionStatement(methodInvocation));
//    // writerStartObject(writer);
//    statements.add(ast.newExpressionStatement(GenUtils.getMethodInvocation(ast, null,
//        CMChangeJsonConvertorGenUtils.mName_WriteStartObject, ParamWriter)));
//
//    // writeString(writer, info.getUdt2asso(), "Udt2asso", serializer);
//    MethodInvocation getValueInvocation = ast.newMethodInvocation();
//    getValueInvocation.setName(ast.newSimpleName("get" + refElement.getLabelID()));
//    getValueInvocation.setExpression(ast.newSimpleName(AssoValName));
//    writeBasicWriteString(
//        ast, refElement, statements, CMChangeJsonConvertorGenUtils.mName_WriteString,
//        getValueInvocation);
//
//    // WriteXXX(...);
//    // WriteXXX(...);
//    ArrayList<Statement> forEachStatement = getForEachStatement();
//    statements.addAll(forEachStatement);
//
//    // writerEndObject(writer);
//    statements.add(ast.newExpressionStatement(GenUtils.getMethodInvocation(ast, null,
//        CMChangeJsonConvertorGenUtils.mName_WriteEndObject, ParamWriter)));
//    Block block = ast.newBlock();
//    block.statements().addAll(statements);
//    return block;
//  }
//
//  private org.eclipse.jdt.core.dom.Statement getNullReturnStatement(
//      List<Statement> beforeReturnlist) {
//    IfStatement ifStatement = ast.newIfStatement();
//    InfixExpression ifCondition = ast.newInfixExpression();
//    ifCondition.setLeftOperand(ast.newSimpleName(AssoValName));
//    ifCondition.setOperator(InfixExpression.Operator.EQUALS);
//    ifCondition.setRightOperand(ast.newNullLiteral());
//    ifStatement.setExpression(ifCondition);
//    Block thenBlock = ast.newBlock();
//    if (beforeReturnlist != null && beforeReturnlist.size() > 0) {
//      thenBlock.statements().addAll(beforeReturnlist);
//    }
//    thenBlock.statements().add(ast.newReturnStatement());
//    ifStatement.setThenStatement(thenBlock);
//    return ifStatement;
//  }
//
//  private static void writeBasicWriteString(
//      AST ast,
//      IGspCommonField element,
//      ArrayList<Statement> result,
//      String methodName,
//      Expression propValueArgument) {
//    MethodInvocation writeInvocation = ast.newMethodInvocation();
//    writeInvocation.setName(ast.newSimpleName(methodName));
//    writeInvocation.arguments().add(ast.newSimpleName(ParamWriter));
//    writeInvocation.arguments().add(propValueArgument);
//    StringLiteral propNameLiteral = ast.newStringLiteral();
//    propNameLiteral.setLiteralValue(element.getLabelID());
//    writeInvocation.arguments().add(propNameLiteral);
//    writeInvocation.arguments().add(ast.newSimpleName(ParamSerializer));
//    result.add(ast.newExpressionStatement(writeInvocation));
//  }
//
//  protected VariableDeclarationStatement getInitStatement() {
//    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
//    variableDeclarationFragment.setName(ast.newSimpleName(AssoValName));
//    CastExpression castExpression = ast.newCastExpression();
//    // TODO: java版临时注释 dbe暂不提供
//    TypeInfo assTypeSyntax =
//        Utils.getRefElementTypeSyntax(
//            dataTypeGenContext.coreGenContext.getCefGenContext(), refElement);
//    castExpression.setType(assTypeSyntax.getType(ast));
//    castExpression.setExpression(ast.newSimpleName(ParamValue));
//    variableDeclarationFragment.setInitializer(castExpression);
//    VariableDeclarationStatement variableDeclarationExpression =
//        ast.newVariableDeclarationStatement(variableDeclarationFragment);
//    variableDeclarationExpression.setType(assTypeSyntax.getType(ast));
//    return variableDeclarationExpression;
//  }
//
//  private ArrayList<Statement> getForEachStatement() {
//    ArrayList<Statement> statements = new ArrayList<>();
//
//    GspFieldCollection elements =
//        refElement.getChildAssociations().get(0).getRefElementCollection();
//
//    for (IGspCommonField refEle : elements) {
//      getWriteItemStatement(refEle, statements);
//    }
//    return statements;
//  }
//
//  // private GspFieldCollection  getFieldCollection(IGspCommonField field)
//  // {
//  //    if (!field.getIsRefElement())
//  //        return field.getChildAssociations()[0].RefElementCollection();
//  //    var refElementId = field.getRefElementId();
//  //    var refModelId = field.ParentAssociation.getRefModelID();
//  //    var refModelPkg = field.ParentAssociation.getRefModelPkgName();
//  //    var refModel =
//  // dataTypeGenContext.coreGenContext.CefGenContext.GetMetadataContent<GspBusinessEntity>(refModelPkg, refModelId);
//  //    var refElement = refModel.FindElementById(refElementId);
//
//  //    return GetFieldCollection(refElement);
//  // }
//
//  private IGspCommonField getRefElement(IGspCommonField field) {
//    if (!field.getIsRefElement()) {
//      return field;
//    }
//
//    String refElementId = field.getRefElementId();
//
//    String refModelId = field.getParentAssociation().getRefModelID();
//
//    String refModelPkg = field.getParentAssociation().getRefModelPkgName();
//
//    GspBusinessEntity refModel =
//        dataTypeGenContext
//            .coreGenContext
//            .getCefGenContext()
//            .getMetadataContent(refModelPkg, refModelId);
//
//    IGspCommonObject refObj =
//        refModel.findObjectById(field.getParentAssociation().getRefObjectID());
//
//    IGspCommonElement refElement = refObj.findElement(refElementId);
//
//    return getRefElement(refElement);
//  }
//
//  private void getWriteItemStatement(IGspCommonField refElement, ArrayList<Statement> block) {
//    MethodInvocation getValueInvocation = ast.newMethodInvocation();
//    getValueInvocation.setName(ast.newSimpleName("get" + refElement.getLabelID()));
//    getValueInvocation.setExpression(ast.newSimpleName(AssoValName));
//    WriteEntityBasicInfoGenerator.writeElementGen(
//        ast, refElement, block, dataTypeGenContext, getValueInvocation);
//    addImport(ICefData.class.getTypeName());
//    // ExpressionSyntax express;
//    // if (refElement.getIsUdt())
//    // {
//    //    express = GetUdtStatement(refElement);
//    // }
//    // else
//    // {
//    //    express = GetElementStatement(refElement);
//    // }
//
//    // var statement = SyntaxFactory.ExpressionStatement(
//    //    SyntaxFactory.AssignmentExpression(
//    //        SyntaxKind.SimpleAssignmentExpression,
//    //        SyntaxFactory.MemberAccessExpression(
//    //            SyntaxKind.SimpleMemberAccessExpression,
//    //            SyntaxFactory.IdentifierName(AssoValName),
//    //            SyntaxFactory.IdentifierName(refElement.getLabelID())), express));
//
//    // return SyntaxFactory.IfStatement(
//    //    SyntaxFactory.BinaryExpression(
//    //        SyntaxKind.LogicalOrExpression,
//    //        GetBinaryExpression(refElement.getLabelID()),
//    //        GetBinaryExpression(StringUtils.ToCamelCase(refElement.getLabelID()))),
//    //    statement
//    //   );
//
//  }
//}
